package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Admin;
import com.example.entity.MS;
import com.example.entity.Student;
import com.example.service.StudentService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

//@CrossOrigin
@RestController
@RequestMapping("students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 学生登录
     * @return
     */
    @PostMapping("/loginStudent")
    public R login(@RequestBody Student student){
        LambdaQueryWrapper<Student> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(student.getStuNumber()),Student::getStuNumber,student.getStuNumber());
        lqw.eq(Strings.isNotEmpty(student.getPassword()),Student::getPassword,student.getPassword());

        Student one = studentService.getOne(lqw);

        if (one ==null){
            return R.error("登录失败！");
        }else if (! "true".equals(one.getStatus())){
            return R.error("登录失败！账号被警用!");
        }else {
            return R.success("登录成功！",one);
        }
    }


    /**
     * 学生分页查询
     * @return
     */
    @GetMapping("/getPageStudent/{current}/{size}")
    public R getPageStudent(@PathVariable int current,@PathVariable int size,Student student){


        IPage page =new Page(current,size);
        LambdaQueryWrapper<Student> lqw =new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(student.getName()),Student::getName,student.getName());

        IPage page1 = studentService.page(page,lqw);

        return R.success("学生分页查询！",page1);
    }

    /**
     * 添加学生
     * @return
     */
    @PostMapping("/addStudent")
    public R addStudent(@RequestBody Student student){

        LambdaQueryWrapper<Student> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(student.getStuNumber()),Student::getStuNumber,student.getStuNumber());
        //1.查询数据库此学号是否存在
        Student one = studentService.getOne(lqw);

        //2.不存在在添加
        if (one==null){
            boolean save = studentService.save(student);
            if (save){
                return R.success("添加学生成功！",null);
            }else {
                return R.error("添加学生失败！");
            }
        }else {
            //3.存在
            return R.error("学号存在！添加失败！");
        }
    }

    /**
     * 通过id查询学生
     * @return
     */
    @GetMapping("/getStudentById")
    public R getStudentById(@PathVariable int id){

        LambdaQueryWrapper<Student> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Student::getId,id);
        Student one = studentService.getOne(lqw);
        return R.success("通过id查询学生",one);
    }

    /**
     * 通过id修改学生
     * @return
     */
    @PutMapping("/updateStudent")
    public R updateStudent(@RequestBody Student student){
        boolean b = studentService.updateById(student);
        if (b){
           return R.success("通过id修改学生成功",student);
        }else {
           return R.error("通过id修改学生失败！");
        }
    }

    /**
     * 通过id修改学生账号状态
     * @return
     */
    @PutMapping("/updateStudentStatus")
    public R updateStudentStatus(@RequestBody Student student){
        boolean b = studentService.updateById(student);
        if (b){
           return R.success("通过id修改学生账号状态成功",student);
        }else {
           return R.error("通过id修改学生账号状态失败！");
        }
    }

    /**
     * 通过id删除学生
     * @return
     */
    @DeleteMapping("/deleteStudent/{id}")
    public R deleteStudent(@PathVariable int id){

        boolean b = studentService.removeById(id);
        if (b){
           return R.success("通过id删除学生成功",id);
        }else {
           return R.error("通过id删除学生失败！");
        }
    }

    /**
     * 根据id查询学生信息
     * @return
     */
    @GetMapping("/getMyself/{id}")
    public R getMyself(@PathVariable int id){
        Student one = studentService.getById(id);
        return R.success("根据id查询学生信息(修改回显)",one);
    }


    /**
     * select major,COUNT(major) as count from student group by major ORDER BY count
     * 可视化（按专业统计注册学生数量）
     */
    @GetMapping("/getStudentByMajor")
    public R getStudentByMajor(){

        List<MS> list = studentService.getStudentByMajor();
        return R.success("按专业统计注册学生数量",list);
    }

}

